# Configuration script for sphinx decoder                  -*-mode:Perl-*-

# Variables starting with $DEC_CFG_ refer to decoder specific
# arguments, those starting with $CFG_ refer to trainer arguments,
# some of them also used by the decoder.

$DEC_CFG_VERBOSE = 1;		# Determines how much goes to the screen.

# These are filled in at configuration time
$DEC_CFG_DB_NAME = 'msu_ru_nsh';
$DEC_CFG_BASE_DIR = '/home/shmyrev/work/msu_ru_nsh_sphinx';
$DEC_CFG_SPHINXDECODER_DIR = '../sphinx3';
$DEC_CFG_SPHINXTRAIN_CFG = './etc/sphinx_train.cfg';

require $DEC_CFG_SPHINXTRAIN_CFG;

$DEC_CFG_BIN_DIR = "$DEC_CFG_BASE_DIR/bin";
$DEC_CFG_GIF_DIR = "$DEC_CFG_BASE_DIR/gifs";
$DEC_CFG_SCRIPT_DIR = "$DEC_CFG_BASE_DIR/scripts_pl";

$DEC_CFG_EXPTNAME = "$DEC_CFG_DB_NAME";
$DEC_CFG_JOBNAME  = "$DEC_CFG_DB_NAME"."_job";

# Models to use.
$DEC_CFG_SENONES = $CFG_N_TIED_STATES;
$DEC_CFG_GAUSSIANS = $CFG_FINAL_NUM_DENSITIES;
# The names below assume sphinx3 models
$DEC_CFG_MODEL_NAME = "$DEC_CFG_EXPTNAME.cd_${CFG_DIRLABEL}_${DEC_CFG_SENONES}_${DEC_CFG_GAUSSIANS}";
$DEC_CFG_MDEF = "$DEC_CFG_EXPTNAME.${DEC_CFG_SENONES}.mdef";

$DEC_CFG_FEATFILES_DIR = "$DEC_CFG_BASE_DIR/feat";
$DEC_CFG_FEATFILE_EXTENSION = '.mfc';
$DEC_CFG_VECTOR_LENGTH = $CFG_VECTOR_LENGTH;
$DEC_CFG_AGC = $CFG_AGC;
$DEC_CFG_CMN = $CFG_CMN;
$DEC_CFG_VARNORM = $CFG_VARNORM;

$DEC_CFG_QMGR_DIR = "$DEC_CFG_BASE_DIR/qmanager";
$DEC_CFG_LOG_DIR = "$DEC_CFG_BASE_DIR/logdir";
$DEC_CFG_MODEL_DIR = "$CFG_MODEL_DIR";

#*******variables used in decoding of wave files *******
$DEC_CFG_DICTIONARY     = "$DEC_CFG_BASE_DIR/etc/${DEC_CFG_DB_NAME}.dic";
$DEC_CFG_FILLERDICT     = "$DEC_CFG_BASE_DIR/etc/$DEC_CFG_DB_NAME.filler";
$DEC_CFG_LISTOFFILES    = "$DEC_CFG_BASE_DIR/etc/${DEC_CFG_DB_NAME}_test.fileids";
$DEC_CFG_TRANSCRIPTFILE = "$DEC_CFG_BASE_DIR/etc/${DEC_CFG_DB_NAME}_test.transcription";

# This variables, used by the decoder, have to be user defined, and
# may affect the decoder output

$DEC_CFG_LANGUAGEMODEL  = "$DEC_CFG_BASE_DIR/etc/msu_ru_nsh.lm.dmp";
$DEC_CFG_LANGUAGEWEIGHT = "10";
$DEC_CFG_BEAMWIDTH = "1e-80";
$DEC_CFG_WORDBEAM = "1e-40";

$DEC_CFG_ALIGN = "sclite";

#*******variables used in characterizing models*******

$DEC_CFG_HMM_TYPE = $CFG_HMM_TYPE;

if (($DEC_CFG_HMM_TYPE ne ".semi.") and ($DEC_CFG_HMM_TYPE ne ".cont.")) {
  die "Please choose one CFG_HMM_TYPE out of '.cont.' or '.semi.', " .
    "currently $DEC_CFG_HMM_TYPE\n";
}

# This comes directly from reading the code. The feature definitions
# aren're represented exactly by the same string in the trainer and
# the decoder. Therefore, we need to map between them.
%feature_type = (
		 'c/1..L-1/,d/1..L-1/,c/0/d/0/dd/0/,dd/1..L-1/' => 's2_4x',
		 'c/1..L-1/d/1..L-1/c/0/d/0/dd/0/dd/1..L-1/'    => 's3_1x39',
		 'c/0..L-1/d/0..L-1/dd/0..L-1/'                 => '1s_c_d_dd',
		 'c/0..L-1/d/0..L-1/'                           => 'cep_dcep',
		 'c/0..L-1/'                                    => 'cep',
		 'c/0..L-1/dd/0..L-1/'                          => 'INVALID',
		 '4s_12c_24d_3p_12dd'                           => 's2_4x',
		 '1s_12c_12d_3p_12dd'                           => 's3_1x39',
		 '1s_c_d_dd'                                    => '1s_c_d_dd',
		 '1s_c_d'                                       => 'cep_dcep',
		 '1s_c'                                         => 'cep',
		 '1s_c_dd'                                      => 'INVALID',
		 '1s_d'                                         => 'INVALID',
		 '1s_dd'                                        => 'INVALID',
		 '1s_c_d_ld_dd'                                 => 'INVALID',
		);

((exists $feature_type{$CFG_FEATURE}) and ($DEC_CFG_FEATURE = $feature_type{$CFG_FEATURE}) 
 or ($DEC_CFG_FEATURE = "INVALID"));

if ($DEC_CFG_FEATURE eq "INVALID") {
  die "Feature type used for training, $CFG_FEATURE, cannot be used for decoding.\n" .
    "Please use one of 4s_12c_24d_3p_12dd, 1s_12c_12d_3p_12dd, 1s_c_d_dd, 1s_c_d, 1s_c\n";
}

$DEC_CFG_NPART = 1;		#  Define how many pieces to split decode in

$DEC_CFG_OKAY_COLOR = '00D000';
$DEC_CFG_WARNING_COLOR = '555500';
$DEC_CFG_ERROR_COLOR = 'DD0000';

sub DEC_DateStr()
{
    my (@time) = localtime(time());
    my ($day_str) = sprintf ("%4d-%02d-%02d %02d:%02d",(1900 + $time[5]), ($time[4]+1),$time[3],$time[2],$time[1]);
}

sub DEC_CreateHeader {
    my $logfile = shift;

    open HTML_LOG,">$logfile";
    print HTML_LOG "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
    print HTML_LOG "<html>\n";
    print HTML_LOG "<head>\n";
    print HTML_LOG "<META HTTP-EQUIV=REFRESH CONTENT=60>\n"; # Force reloading every 60 seconds
    print HTML_LOG "<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">\n";
    print HTML_LOG "<title>$DEC_CFG_EXPTNAME</title>\n";
    print HTML_LOG "</head>\n";
    print HTML_LOG "<body BGCOLOR = \"#F0F0F5\">\n";
    print HTML_LOG "<pre>\n";
    print HTML_LOG "<CENTER><h1>${CFG_BASE_DIR}/${CFG_EXPTNAME}</h1></CENTER>\n";
    print HTML_LOG "<CENTER> <h3>$hostname</h3></CENTER>\n";
    print HTML_LOG "<hr>\n";
    close HTML_LOG;
}

sub DEC_Log ()
{
    my $message = shift;
    my (@time) = localtime(time());
    my ($day_str) = sprintf ("%4d-%02d-%02d %02d:%02d",(1900 + $time[5]), ($time[4]+1),$time[3],$time[2],$time[1]);
    my $logfile = "$DEC_CFG_BASE_DIR/$DEC_CFG_EXPTNAME.html";

    print "$message" if $DEC_CFG_VERBOSE;
    if (!-s $logfile) {
        DEC_CreateHeader($logfile);
    }
    open HTML_LOG,">>$logfile";
    if ($message =~ m/^(MODULE:).*/) {
        print HTML_LOG "<hr>\n";
        chomp ($message);
	print HTML_LOG "<b>$message\t($day_str)</b>\n";	# Put Date information on Module headers
    } else {
        print HTML_LOG "$message";
    }
    close HTML_LOG;
}

sub DEC_HTML_Print () 
{
    my $message = shift;
    my $logfile = "$DEC_CFG_BASE_DIR/$DEC_CFG_EXPTNAME.html";

    if (! -s $logfile) {
        DEC_CreateHeader($logfile);
    }
    open HTML_LOG,">>$logfile";
    print HTML_LOG "$message";
    close HTML_LOG;

}

sub DEC_FormatURL ()
{
    my $path = shift;
    my $label = shift;
    my $url;

    if ($path =~ m/^\/.*/) {
        $url = "file://";
    } else {
        $url = "file:///";
    }
    $url = "<a href=\"" . $url . "$path\">$label</a>";
    return $url;
}

sub DEC_ImgSrc ()
{
    my $path = shift;
    my $url;

    if ($path =~ m/^\/.*/) {
        $url = "file://";
    } else {
        $url = "file:///";
    }
    $url = "<img src=\"" . $url . "$path\">";
    return $url;
}

sub DEC_LogWarning ()
{
    my $message = shift;
    my (@time) = localtime(time());
    my ($day_str) = sprintf ("%4d-%02d-%02d %02d:%02d",(1900 + $time[5]), ($time[4]+1),$time[3],$time[2],$time[1]);
    my $logfile = "$DEC_CFG_BASE_DIR/$DEC_CFG_EXPTNAME.html";

    print "WARNING: $message" if $DEC_CFG_VERBOSE;
    if (! -s $logfile) {
        DEC_CreateHeader($logfile);
    }
    open HTML_LOG,">>$logfile";
    print HTML_LOG "<p>WARNING: $message</p>";
    close HTML_LOG;
}

sub DEC_LogError ()
{
    my $message = shift;
    my (@time) = localtime(time());
    my ($day_str) = sprintf ("%4d-%02d-%02d %02d:%02d",(1900 + $time[5]), ($time[4]+1),$time[3],$time[2],$time[1]);
    my $logfile = "$DEC_CFG_BASE_DIR/$DEC_CFG_EXPTNAME.html";

    chomp ($message);		# remove \n if it exists

    print "ERROR: $message" if $DEC_CFG_VERBOSE;
    if (! -s $logfile) {
        DEC_CreateHeader($logfile);
    }
    open HTML_LOG,">>$logfile";
    print HTML_LOG "<p>ERROR: $message</p>";
    close HTML_LOG;
}





return 1;
